﻿using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;


public class Receiver : MonoBehaviour
{
    public InputField inf_Duration;
    public Image img_ImageDisplay;

    public Sprite stimulusImage;

    public void OnStartBtnClicked()
    {
        if(inf_Duration.text != null)
        {
            float duration;

            try
            {
                duration = float.Parse(inf_Duration.text);
            }
            catch
            {
                duration = 10f;
            }

            Debug.Log(duration);

            GetComponent<Controller>().Init();
        }
        else
        {
            Debug.Log("fail");
        }

    }

    public void OnResourceReloadBtnClicked()
    {
        string path = FileDialog.OpenFile();
        if (path != null)
        {
            Debug.Log(path);
            StartCoroutine(ResourceLoad(path));
        }
    }

    IEnumerator ResourceLoad(string path)
    {
        UnityWebRequest request = UnityWebRequestTexture.GetTexture(path);
        yield return request.SendWebRequest();
        if (request.isHttpError || request.isNetworkError)
        {
            Debug.Log(request.error);
        }
        else
        {
            Texture tt = DownloadHandlerTexture.GetContent(request);

            stimulusImage = Sprite.Create((Texture2D)tt, new Rect(0, 0, tt.width, tt.height), new Vector2(0.5f, 0.5f));
            img_ImageDisplay.sprite = stimulusImage;
        }
    }

}



